package com.kl.controller;


import com.kl.domain.UserScoreDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.UserScore;
import com.kl.service.IUserScoreService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-29
 */
@RestController
@RequestMapping("/userScoreController")
public class UserScoreController {

    @Autowired
    private IUserScoreService userScoreService;

    //积分查询接口
    @ApiOperation(value = "用户积分查询接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType = "query")
    })
    @GetMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result<UserScoreDTO> selectByScoreId(@RequestParam Long userId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        UserScoreDTO userScoreDTO = new UserScoreDTO();
        try {
            //pageList = userMapper.selectPage(page, null);
            UserScore userScore = userScoreService.getById(userId);
            BeanUtils.copyProperties(userScore,userScoreDTO);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS).setResult(userScoreDTO);
    }

    //积分增加接口
    @ApiOperation(value = "用户积分增加接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType = "query"),
            @ApiImplicitParam(name = "score",value = "前台获取的积分",required = true,paramType = "query")
    })
    @PostMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result inserInto(@RequestParam("userId") Long userId,@RequestParam("score") Integer score){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            userScoreService.inserInto(userId,score);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }

    //积分修改接口
    @ApiOperation(value = "用户积分修改接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType = "query"),
            @ApiImplicitParam(name = "score",value = "前台消费的积分",required = true,paramType = "query")
    })
    @PutMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result updateInfo(@RequestParam Long userId,@RequestParam Integer score){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        return userScoreService.updateInfo(userId, score);
    }

    //积分删除接口
    @ApiOperation(value = "用户积分删除接口")
    @ApiImplicitParams({
            //name为方法中的参数名，value就是它的含义，required 是否必填
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType = "query")
    })
    @DeleteMapping// @ResponseBody  将后台的数据以json的格式返回前台
    public Result deleteByScoreId(@RequestParam Long userId){// @RequestBody  将前端传入的json格式的数据绑定到实体类
        try {
            userScoreService.removeById(userId);
        }catch (Exception e){
            e.printStackTrace();
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.SUCCESS);
    }
}
